<?php
	include 'functions.php';
	include 'Mail.php';
	$firstNameErr = $lastNameErr = $emailErr = $usernameErr = $password1Err = $password2Err = $passwordMatchErr = $addressErr = $zipcodeErr = $cityErr = "";
	$firstName = $lastName = $email = $username = $password1 = $password2 = $address = $zipcode = $city = "";
	$hasErrors = FALSE;
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		if(empty($_POST["firstname"])){
			$firstNameErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$firstName = ucfirst($_POST["firstname"]);
		}
		if(empty($_POST["lastname"])){
			$lastNameErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$lastName = ucfirst($_POST["lastname"]);
		}
		if(empty($_POST["email"])){
			$emailErr = "Missing";
			$hasErrors = TRUE;
		}
		elseif(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
			$emailErr = "Invalid Email";
			$hasErrors = TRUE;
		}
		else{
			$email = $_POST["email"];
		}
		if(empty($_POST["username"])){
			$usernameErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$username = $_POST["username"];
		}
		if(empty($_POST["password1"])){
			$password1Err = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$password1 = $_POST["password1"];
		}
		if(empty($_POST["password2"])){
			$password2Err = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$password2 = $_POST["password2"];
		}
		if(empty($_POST["address"])){
			$addressErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$address = $_POST["address"];
		}
		if(empty($_POST["city"])){
			$cityErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$city = $_POST["city"];
		}
		if(empty($_POST["zipcode"])){
			$zipcodeErr = "Missing";
			$hasErrors = TRUE;
		}
		else{
			$zipcode = $_POST["zipcode"];
		}
		if (is_numeric($_POST["zipcode"])){
			$zipcode = $_POST["zipcode"];
		}
		else{
			$zipcodeErr = "Invalid zipcode";
			$hasErrors = TRUE;
		}
		if (strlen($_POST["zipcode"]) != 5){
			$zipcodeErr = "Incorrect length";
			$hasErrors = TRUE;
		}
		else{
			$zipcode = $_POST["zipcode"];
		}
		if($_POST["password1"] != $_POST["password2"]){
			$passwordMatchErr = "Not matching";
			$hasErrors = TRUE;
		}
		if(!$hasErrors){
			$con = mysqli_connect('localhost','mrc02_login','loginDB','mrc0260_sportinggoodscompany') or die(mysql_error());
			$password = md5($password1);
			$hash = md5(rand(0,100));
			$date = date("Y-m-d H:i:s");
			$query = "INSERT INTO sgc_users VALUES('$firstName','$lastName', '$address', '$city', '$_POST[state]', '$zipcode', '$email','$username','$password','$date', 0, '$hash')";
			mysqli_query($con, $query) or die(mysqli_error($con));
                        mysqli_close($con);
			send_email($email, $hash, $_POST["password1"], $username);
		        header('Refresh: 0; URL=welcome.php');

		}
	}
	
?>
<html>
	<head>
	<title>Registration</title>
	<link rel="stylesheet" type="text/css" href="sitestyle.css" />
	</head>
	
	<body>
	<div id="wrapper">
		<div id="header">
			<?php login(); ?>
		</div>
		<div id="middle">
			<div id="navbar">
			.
			</div>
			<div id="content">
				<div id="formdiv">
					<form action="registration.php" method="post">
						<ul class="formlist">
						<div id="leftformdiv">
							<li><label class="formlabel" for="firstname">First Name</label> 
							<input type="text" name="firstname" value="<?php echo htmlspecialchars($firstName);?>">
								<span class="error"><?php echo $firstNameErr;?></span></li><br>
							<li><label class="formlabel" for="lastname">Last Name</label>	
							<input type="text" name="lastname" value="<?php echo htmlspecialchars($lastName);?>">
								<span class="error"><?php echo $lastNameErr;?></span></li><br>
							<li><label class="formlabel" for="email">Email</label>
							<input type="text" name="email" value="<?php echo htmlspecialchars($email);?>">
								<span class="error"><?php echo $emailErr;?></span></li><br>
							<li><label class="formlabel" for="username">Username</label>
							<input type="text" name="username" value="<?php echo htmlspecialchars($username);?>">
								<span class="error"><?php echo $usernameErr;?></span></li><br>
							<li><label class="formlabel" for="password1">Password</label>
							<input type="password" name="password1" value="<?php echo htmlspecialchars($password1);?>">
								<span class="error"><?php echo $password1Err;?></span></li><br>
							<li><label class="formlabel" for="password2">Re-enter Password</label>
							<input type="password" name="password2" value="<?php echo htmlspecialchars($password2);?>">
								<span class="error"><?php echo $passwordMatchErr;?></span></li><br>
							</div>
							<div id="rightformdiv">
							<li><label class="formlabel">Street Address:</label>
							<input type="text" name="address" value="<?php echo htmlspecialchars($address);?>">
								<span class="error"><?php echo $addressErr;?></span></li><br>
							<li><label class="formlabel">City :</label>
							<input type="text" name="city" value="<?php echo htmlspecialchars($city);?>">
								<span class="error"><?php echo $cityErr;?></span></li>								
							<li><label class="formlabel">State/Zipcode:</label> 
							<?php list_states();?>
							<input type="text" name="zipcode" value="<?php echo htmlspecialchars($zipcode);?>" size="7">
								<span class="error"><?php echo $zipcodeErr;?></span></li><br>
							</ul>
							<input type="submit" name="Submit" value="Register" class="submit">
							</div>
					</form>
				</div>
			</div>
		</div>
		<div id="footer">
		</div>
	</div>
	</body>
</html>
